/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web;

import java.io.IOException;

/**
 * Represents the HTML tag.
 * @author J Patrick Davenport
 *
 */
public class Html extends ChildbearingPageElement implements ILanguageAttributes<Html> {
	private static final String	TRANSITION_DTD				= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
	private static final String	HTTP_WWW_W3_ORG_1999_XHTML	= "http://www.w3.org/1999/xhtml";
	private static final String	XMLNS_KEY					= "xmlns";
	private static final String	HTML						= "html";
	private Head				head;
	private Body				body;

	{
		this.head = new Head();
		this.body = new Body();
		super.addPageElement(this.head);
		super.addPageElement(this.body);
		this.setTag(HTML);
		this.setSupportsAttributes(true);
		this.setAttribute(XMLNS_KEY, HTTP_WWW_W3_ORG_1999_XHTML);
	}

	public Html setPageTitle(final String _title) {
		this.head.setTitle(_title);
		return this;
	}

	@Override
	public Html setDir(final String _value) {
		this.setAttribute(DIR_KEY, _value);
		return this;
	}

	@Override
	public Html setLang(final String _lang) {
		this.setAttribute(LANG_KEY, _lang);
		return this;
	}

	@Override
	public Html setXmlLang(final String _xmlLang) {
		this.setAttribute(XML_LANG_KEY, _xmlLang);
		return this;
	}

	public String getPageTitle() {
		return this.head.getTitle();
	}

	@Override
	public String getDir() {
		return this.getAttributeValue(DIR_KEY);
	}

	@Override
	public String getLang() {
		return this.getAttributeValue(LANG_KEY);
	}

	@Override
	public String getXmlLang() {
		return this.getAttributeValue(XML_LANG_KEY);
	}

	public Head getHead() {
		return this.head;
	}

	public void setHead(final Head _head) {
		this.head = _head;
	}

	public Body getBody() {
		return this.body;
	}

	public void setBody(final Body _body) {
		this.body = _body;
	}

	@Override
	public void render() throws IOException {
		this.getWriter().println(TRANSITION_DTD);
		super.render();
	}

	/**
	 * Does not add any elements since Body and Head are autogenerated.
	 */
	@Override
	public Html addPageElement(final PageElement _element) {
		return this;
	}
}
